<template>
  <!--
          作者：luoyiming
          时间：2019-10-25
          描述：会员-用户列表
      -->
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="formInline" class="demo-form-inline">

        <el-form-item label="区域">
        <el-select  v-model="formInline.province_id" placeholder="省" @change="initCity" clearable>
          <el-option :label="item.name" :value="item.id" v-for="(item,index) in areaList" :key='index'></el-option>
        </el-select>
        <el-select  v-if="formInline.province_id!=''" v-model="formInline.city_id" placeholder="市" @change="initRegion" clearable>
          <el-option :label="item1.name" :value="item1.id" v-for="(item1,index1) in areaList[formInline.province_id]['city']"
                     :key='index1'></el-option>
        </el-select>

        
        <!-- <el-select  v-if="formInline.city_id!=''" v-model="formInline.region_id" placeholder="区" clearable>
          <el-option :label="item2.name" :value="item2.id"
                     v-for="(item2,index2) in areaList[formInline.province_id]['city'][formInline.city_id]['region']"
                     :key='index2'></el-option>
        </el-select> -->
      </el-form-item>

      <el-form-item label="商户">
          <el-select size="small" v-model="formInline.shop_supplier_id" placeholder="请选择">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(item, index) in supplier_list" :key="index" :label="item.name" :value="item.shop_supplier_id"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="关键词">
          <el-input v-model="formInline.nick_name" placeholder="请输入昵称|手机号|ID"></el-input>
        </el-form-item>
        <!-- <el-form-item label="会员等级">
          <el-select v-model="formInline.grade_id" placeholder="请选择">
            <el-option label="全部" value="0"></el-option>
            <el-option v-for="(item, index) in gradeList" :key="index" :label="item.name" :value="item.grade_id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="来源">
          <el-select v-model="formInline.reg_source" placeholder="请选择">
            <el-option label="全部" value=""></el-option>
            <el-option label="小程序" value="wx"></el-option>
            <el-option label="APP" value="app"></el-option>
            <el-option label="H5" value="h5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="formInline.reg_golden" placeholder="请选择">
            <el-option label="全部" :value="0"></el-option>
            <el-option label="金牌推广员" :value="1"></el-option>
            <el-option label="普通会员" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="注册时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker v-model="formInline.reg_date" type="daterange" value-format="yyyy-MM-dd"
                            range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </div>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>


        <el-form-item label="订单创建时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="formInline.order_create_time"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button size="small" type="success" @click="onExportUser">导出会员数据</el-button>
        </el-form-item>

        <!-- <el-form-item label="用户ID">
          <el-input v-model="formInline.user_id" placeholder="请输入用户ID"></el-input>
        </el-form-item> -->

        <el-form-item>
          <el-button size="small" type="success" @click="onExport">导出会员业绩</el-button>
        </el-form-item>
      </el-form>
      <!--      <div class="mb16">-->
      <!--        <span>标签</span>-->
      <!--        <span class="pl10">-->
      <!--          <el-radio-group v-model="formInline.tag_id" size="mini" @change="onSubmit">-->
      <!--            <el-radio-button label="0">全部</el-radio-button>-->
      <!--            <el-radio-button :label="item.tag_id" v-for="(item, index) in allTag" :key="index">-->
      <!--              {{item.tag_name}}-->
      <!--            </el-radio-button>-->
      <!--          </el-radio-group>-->
      <!--        </span>-->
      <!--      </div>-->
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="user_id" label="ID" width="80"></el-table-column>
          <el-table-column prop="nickName" label="昵称"></el-table-column>
          <el-table-column prop="nickName" label="微信头像">
            <template slot-scope="scope">
              <img :src="scope.row.avatarUrl" width="30px" height="30px"/>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="" label="会员等级">
            <template slot-scope="scope">
              <span v-if="scope.row.grade_id == 0">无等级</span>
              <span v-else>{{ scope.row.grade.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="mobile" label="来源">
            <template slot-scope="scope">
              <span v-if="scope.row.reg_source == 'wx'">小程序</span>
              <span v-if="scope.row.reg_source == 'app'">APP</span>
              <span v-if="scope.row.reg_source == 'h5'">H5</span>
            </template>
          </el-table-column>
          <el-table-column prop="points" label="积分"></el-table-column>
          <el-table-column prop="balance" label="余额"></el-table-column> -->
          <el-table-column  prop="agent_apply.real_name"  label="姓名"> </el-table-column>
          <el-table-column  prop="agent_apply.mobile"  label="入驻手机号"> </el-table-column>

          <el-table-column prop="total_invite" label="邀请人数">
            <template slot-scope="scope"><span @click="refereeClick(scope.row)" style="cursor: pointer;color: #00a0e9">{{ scope.row.total_invite }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="total_invite_money" label="邀请购物总额"></el-table-column>
          <el-table-column prop="total_invite_num" label="推荐订单数量">
            <template slot-scope="scope"><span
              style="cursor: pointer;color: #00a0e9" @click="ordersClick(scope.row)">{{ scope.row.total_invite_num }}</span></template>
          </el-table-column>
          <el-table-column prop="agent_apply.region" label="区域" ></el-table-column>
          
          <el-table-column  label="所属商户">
            <template slot-scope="scope">
              <p v-for="item in scope.row.shop_supplier_name">{{item }}</p>
            </template>
          </el-table-column>
          
          <el-table-column prop="agent_apply.create_time" label="申请时间" width="140"></el-table-column>
          <!-- <el-table-column fixed="right" label="操作" width="170">
            <template slot-scope="scope"> -->
          <!--              <el-button @click="addClick(scope.row)" type="text" size="small" v-auth="'/user/user/recharge'">充值-->
          <!--              </el-button>-->
          <!-- <el-button @click="editClick(scope.row)" type="text" size="small" v-auth="'/user/user/edit'">编辑
          </el-button> -->
          <!--              <el-button @click="gradeClick(scope.row)" type="text" size="small" v-auth="'/user/user/grade'">等级-->
          <!--              </el-button>-->
          <!--              <el-button @click="tagClick(scope.row)" type="text" size="small" v-auth="'/user/user/delete'">标签-->
          <!--              </el-button>-->
          <!-- <el-button @click="deleteClick(scope.row)" type="text" size="small" v-auth="'/user/user/delete'">删除
          </el-button> -->
          <!-- </template>
        </el-table-column> -->
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
                       :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                       :total="totalDataNumber"></el-pagination>
      </div>
    </div>

    <!--会员充值-->
    <Recharge v-if="open_add" :open_add="open_add" :form="userModel" :gradeList="gradeList"
              @closeDialog="closeDialogFunc($event, 'add')"></Recharge>
    <!--会员等级-->
    <Grade v-if="open_edit" :open_edit="open_edit" :form="userModel" :gradeList="gradeList"
           @closeDialog="closeDialogFunc($event, 'edit')"></Grade>
    <!--会员标签-->
    <Tag v-if="open_tag" :open_tag="open_tag" :form="userModel" @closeDialog="closeDialogFunc($event, 'tag')"></Tag>
    <Referee :open_referee.sync="open_referee" :referee_id="open_referee_id" :nickName="nickName"></Referee>
    <Orders :open_orders.sync="open_orders" :user_id="0" :referee_id="open_order_referee_id" :nickName="nickName"></Orders>
  </div>
</template>

<script>
import UserApi from '@/api/user.js';
import Grade from './dialog/Grade.vue';
import Recharge from './dialog/Recharge.vue';
import Tag from './dialog/Tag.vue';
import Referee from './dialog/Referee.vue';
import qs from 'qs';
import Orders from "@/views/user/user/dialog/Orders.vue";
import DataApi from '@/api/data.js';
import OrderApi from '@/api/order.js';
export default {
  components: {
    Orders,
    /*编辑组件*/
    Grade,
    Recharge,
    Tag,
    Referee
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      searchForm: {
        order_create_time: '',
        user_id: ''
      },
      formInline: {
        nick_name: '',
        grade_id: '',
        reg_date: '',
        tag_id: 0,
        reg_source: '',
        reg_golden: 1,

        province_id: '',
        city_id: '',
        region_id:'',
      
        order_create_time: '',
        user_id: '',
        shop_supplier_id:[],
        
      },

      supplier_list:[],
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      /*等级*/
      gradeList: [],
      open_tag: false,
      /*所有标签*/
      allTag: [],
      open_referee: false,
      open_referee_id: 0,
      nickName: '',
      open_orders: false,
      open_order_referee_id: 0,
      areaList:[],
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
    this.getRegion()
    this.getSupplierList()
  },
  methods: {

    getSupplierList() {
      let params = this.form;
      OrderApi.getSupplierList(params, true)
        .then(data => {
          this.supplier_list = data.data.list
        })
        .catch(error => {

        });
    },
     /*初始化城市id*/
     initProvince() {
      this.formInline.province_id = ''
    },

    /*初始化城市id*/
    initCity() {
      this.formInline.city_id = ''
    },
    /*初始化区id*/
    initRegion() {
      this.formInline.region_id = ''
    },

    getRegion(){
        let self = this;
        DataApi.getRegion({}, true)
          .then(res => {
            self.areaList = res.data.regionData;
          })
          .catch(error => {

          });
    },

    onExport: function () {
      let baseUrl =
        process.env.NODE_ENV === "development"
          ? window.location.protocol + "//" + window.location.host
          : process.env.VUE_APP_API_URL;
      // let baseUrl = 'https://api.jiumaioda.com/';
      window.location.href = baseUrl + '/index.php/shop/user.user/exportOrder?' + qs.stringify(this.formInline);
    },

    onExportUser: function () {
      let baseUrl = process.env.NODE_ENV === "development"
        ? window.location.protocol + "//" + window.location.host
        : process.env.VUE_APP_API_URL;

      window.location.href = baseUrl + '/index.php/shop/user.user/exportUser?' + qs.stringify(this.formInline);
    },

    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      UserApi.userlist(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
          self.gradeList = data.data.grade;
          self.allTag = data.data.allTag;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*搜索查询*/
    onSubmit() {
      let self = this;
      self.loading = true;
      self.curPage = 1;
      let Params = self.formInline;
      self.getTableList();
    },

    /*打开添加*/
    addClick(item) {
      this.userModel = item;
      this.open_add = true;
    },
    /*打开编辑*/
    editClick(item) {
      let self = this;
      let params = item.user_id;
      this.$router.push({
        path: '/user/user/edit',
        query: {
          user_id: params
        }
      });
    },
    /*打开编辑*/
    gradeClick(item) {
      this.userModel = item;
      this.open_edit = true;
    },

    tagClick(item) {
      this.userModel = item;
      this.open_tag = true;
    },

    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == 'add') {
        this.open_add = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
      if (f == 'edit') {
        this.open_edit = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
      if (f == 'tag') {
        this.open_tag = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
    },

    /*删除用户*/
    deleteClick(row) {
      let self = this;
      self
        .$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          self.loading = true;
          UserApi.deleteuser({
              user_id: row.user_id
            },
            true
          )
            .then(data => {
              self.loading = false;
              if (data.code == 1) {
                self.$message({
                  message: '恭喜你，用户删除成功',
                  type: 'success'
                });
                self.getTableList();
              } else {
                self.loading = false;
              }
            })
            .catch(error => {
              self.loading = false;
            });
        })
        .catch(() => {
          self.loading = false;
        });
    },
    refereeClick(item) {
      this.open_referee_id = item.user_id;
      this.nickName = item.nickName == "微信用户" ? "微信用户" + item.user_id : item.nickName;
      this.open_referee = true;
    },
    ordersClick(item) {
      this.open_order_referee_id = item.user_id;
      this.nickName = item.nickName == "微信用户" ? "微信用户" + item.user_id : item.nickName;
      this.open_orders = true;
    }
  }
};
</script>
<style></style>
